package cn.edu.scau.service.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.scau.dao.BedMapper;
import cn.edu.scau.dao.LiveInRegisterMapper;
import cn.edu.scau.entity.Booking;
import cn.edu.scau.entity.Customer;
import cn.edu.scau.entity.LiveInRegister;
import cn.edu.scau.service.BedService;
import cn.edu.scau.service.BookingService;
import cn.edu.scau.service.CustomerService;
import cn.edu.scau.service.LiveInRegisterService;
@Service
public class LiveInRegisterServiceImpl implements LiveInRegisterService{
	@Autowired
	LiveInRegisterMapper liveInRegisterMapper;
	@Autowired
	CustomerService customerService;
	@Autowired
	BedService bedService;
	@Autowired
	BookingService bookingService;
	
	@Override
	public List<LiveInRegister> selectByNurseID(Integer nid) {
		List<Customer> customers = customerService.selectByNurseId(nid);
		List<LiveInRegister> liveInRegisters = new ArrayList<LiveInRegister>();
		for(Customer c : customers) {
			liveInRegisters.addAll(liveInRegisterMapper.selectByCustomerID(c.getCustomerid()));
		}
		return liveInRegisters;
	}

	@Override
	public List<LiveInRegister> findByIdName(Integer id, String name) {
		List<Customer> customers = customerService.find(id, name);
		List<LiveInRegister> liveInRegisters = new ArrayList<LiveInRegister>();
		for(Customer c : customers) {
			liveInRegisters.addAll(liveInRegisterMapper.selectByCustomerID(c.getCustomerid()));
		}
		return liveInRegisters;
	}

	
	@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	@Override
	public boolean updateFlag(Integer cid) {
		//此处列表中应只有一个记录
		List<LiveInRegister> liveInRegisters = liveInRegisterMapper.findLatestById(cid);
		
		Date thisDate = new Date();
		try {
			for(LiveInRegister i:liveInRegisters) {
				//更新实际出院日期
				i.setActuallyliveoutdate(thisDate);
				//更新可用flag
				i.setRegisteruseflag(false);
				//更新床位状态
				bedService.releaseBed(i.getBedid());
				//上传入院记录
				liveInRegisterMapper.updateByPrimaryKeySelective(i);
			}
		} catch (Exception e) {
			//如果更新失败返回false
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	@Override
	public boolean insert(LiveInRegister record,BigDecimal bookingamount) {
		// 检测外键
		if(null==customerService.selectByPrimaryKey(record.getCustomerid()))
			return false;
		if(bedService.checkOccupiedById(record.getBedid())) {
			return false;
		}else {
			bedService.occupyBed(record.getBedid());
		}
			
		Booking booking = new Booking(null,bookingamount);
		if(!bookingService.insert(booking)) {
			return false;
		}else {
			record.setBookingid(booking.getBookingid());
		}
		
		return 1==liveInRegisterMapper.insertSelective(record);
	}

	@Override
	public List<LiveInRegister> selectAll() {
		return liveInRegisterMapper.selectAll();
	}

	
}
